﻿using Pathfinding;
using Pathfinding.RVO;
using UnityEngine;

namespace NpcFramework
{
    public class CustomNpcSeeker : NpcSeeker
    {
        public Seeker Seeker;
        public CharacterController CharacterController;

        public RVOController RvoController;
        private NavmeshCut _navmeshCut;
        private bool onTargetReached = true;

        private void Awake()
        {
            base.OnAwake();
            aiPath.onReached += OnTargetReached;
        }

        private void OnDestroy()
        {
            aiPath.onReached -= OnTargetReached;
        }

        private void OnTargetReached()
        {
            onTargetReached = true;
        }

        public override bool ReachedEndOfPath()
        {
            return onTargetReached;
        }

        public override void ResetDestination()
        {
            base.ResetDestination();
            onTargetReached = false;
            aiPath.destination = _destination;
            aiPath.SearchPath();
        }

        public override void ResetTargetEndStatus(bool status)
        {
            onTargetReached = status;
        }

        public override void SetDestination(Vector3 destination)
        {
            if (Vector3.Distance(destination, Transform.position) < 0.01f)
            {
                onTargetReached = true;
                return;
            }
            onTargetReached = false;
            base.SetDestination(destination);
            //aiPath.destination = destination;
            //aiPath.SearchPath();
        }

        public override void SetMaxSpeed(float speed)
        {
            if (speed > 0)
            {
                if (_navmeshCut != null)
                {
                    Destroy(_navmeshCut);
                    AstarPath.active.navmeshUpdates.ForceUpdate();
                }
            }
            else
            {
                if (_navmeshCut == null)
                {
                    _navmeshCut = Seeker.gameObject.AddComponent<NavmeshCut>();
                    _navmeshCut.rectangleSize = new Vector2(0.6f, 0.6f);
                    _navmeshCut.useRotationAndScale = true;
                }
            }
            aiPath.maxSpeed = speed;

            if (NFMgr.DataManager.IsMainCharactor(ID))
            {
                RvoController.priority = 1.0f;
            }
            else
            {
                // 解决速度不一样还尾随的问题(RVOAgent.GenerateNeighbourAgentVOs)
                RvoController.priority = Mathf.Min(0.85f, Mathf.Max(0.1f, (speed - 1.2f) * 2f));
            }

        }

        public override float GetPriority()
        {
            return RvoController.priority;
        }

        public override void SwitchRotation(bool enabled)
        {
            aiPath.enableRotation = enabled;
        }

        // public Vector3 GetNextPosition()
        // {
        //     return aiPath.NextPosition;
        // }
        // public Vector3 GetCurrentPosition()
        // {
        //     return aiPath.CurrentPosition;
        // }
        // public void SetNextPosition(Vector3 position)
        // {
        //     aiPath.AnimNextPosition = position;
        // }
        public float GetRemainingDistance()
        {
            return aiPath.remainingDistance;
        }
        public float GetRadius()
        {
            return aiPath.radius;
        }
        
    }
}
